package com.recipe.controller;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.recipe.common.entity.Result;
import com.recipe.entity.User;
import com.recipe.entity.vo.LoginResponse;
import com.recipe.entity.vo.LoginVo;
import com.recipe.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
/**
 * (User)表控制层
 *
 * @author lzk
 * @since 2024-06-14 11:37:48
 */
@RestController
@RequestMapping("user")
@RequiredArgsConstructor
@Slf4j
public class UserController {
    private final UserService userService;




    @PostMapping("/login")
    public Result<LoginResponse> Login(@RequestBody LoginVo loginVo){
        if (log.isInfoEnabled()) {
            log.info("UserController.LoginVo:{}", JSON.toJSONString(loginVo));
        }
        Preconditions.checkNotNull(loginVo.getNickname(), "用户昵称不为空");
        Preconditions.checkNotNull(loginVo.getPassword(), "密码不为空");
        LoginResponse loginResponse =  userService.isLogin(loginVo);
        return Result.ok(loginResponse);
    }
    @PostMapping("/register")
    @SneakyThrows
    public Result<Boolean> Register(@RequestBody User user){
        if (log.isInfoEnabled()) {
            log.info("UserController.register:{}", JSON.toJSONString(user));
        }
        Preconditions.checkNotNull(user.getPassword(), "密码不为空");
        Preconditions.checkNotNull(user.getPhone(), "手机不为空");
        String msg= userService.register(user);
        return Result.ok(msg);
    }
    @GetMapping("/getUserInfo/{username}")
    public Result<User> getUserInfo(@PathVariable String username){
        if (log.isInfoEnabled()) {
            log.info("UserController.getUserInfo:{}",username);
        }
        Preconditions.checkNotNull(username, "用户名不为空");
        User user = userService.getUserByUserName(username);
        return Result.ok(user);
    }
    @PostMapping("updateUserInfo")
    public Result<User> updateUserInfo(@RequestBody User user){
        if (log.isInfoEnabled()) {
            log.info("UserController.updateUserInfo:{}", JSON.toJSONString(user));
        }
        return Result.ok(userService.updateById(user));
    }



}

